﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fly.FastReflection;
using FlyTest;

namespace FastReflectionTest
{
    public partial class WebFormTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page_DataInit();
        }

        protected void Page_DataInit()
        {
            var obj = new User() { UserId = 125 };
            var propertyInfo = obj.GetType().GetProperty("UserId");
            var accessor = FastReflectionCaches.PropertyAccessorCache.Get(propertyInfo);
            var watch = new Stopwatch();
            StringBuilder str = new StringBuilder();
            watch.Start();
            for (int i = 0; i < 2; i++)
            {
                var value = obj.UserId;
            }

            watch.Stop();
            str.Append("直接调用耗时：" + watch.Elapsed + "<br/>");
            watch.Reset();

            // Reflection
            watch.Start();
            for (int i = 0; i < 2; i++)
            {
                var value = (int)propertyInfo.GetValue(obj, null);
            }
            watch.Stop();
            str.Append("使用反射耗时：" + watch.Elapsed + "<br/>");
            watch.Reset();

            // Fast Reflection
            watch.Start();
            for (int i = 0; i < 2; i++)
            {
                var value = (int)propertyInfo.FastGetValue(obj);
            }
            watch.Stop();
            str.Append("使用快速反射扩展耗时：" + watch.Elapsed + "<br/>");
            watch.Reset();

            // Raw Fast Reflection
            watch.Start();
            for (int i = 0; i < 2; i++)
            {
                var value = (int)accessor.GetValue(obj);
            }
            watch.Stop();
            str.Append("使用快速反射耗时：" + watch.Elapsed + "<br/>");
            watch.Reset();
            Literal1.Text = str.ToString();
        }
    }
}